require(['./config'],() =>{
    require(['template','header','footer','bootstrap'],(template,header) =>{
        class ShopCar {
            constructor () {
                this.rendonShopCar().then(() =>{
                    this.delete()
                    this.reduce()
                    this.add()
                    this.celeMoney()
                    this.changeCheck()
                    this.checkAll()
                })
            }
            rendonShopCar () {
                return new Promise((reslove) =>{
                    this.cart = localStorage.getItem('cart')
                    // cart = Array.from(JSON.parse(cart))
                    if(this.cart){
                        $('.section .no-goods').addClass('appear')
                        $('.section .top-bar').removeClass('appear')
                        $('.section .good-list').removeClass('appear')
                        this.cart =JSON.parse(this.cart)
                        console.log(this.cart)
                        $('#goodlist').html(template('goodlisttemplate',{cart:this.cart}))
                        reslove()
                    }else{
                        $('.section .no-goods').removeClass('appear')
                        $('.section .top-bar').addClass('appear')
                        $('.section .good-list').addClass('appear')
                    }
                })
            }
            delete() {
                var _this = this
                $('#goodlist').on('click','li',function () {
                    if($(this).hasClass('x')){
                        if(confirm('确定要删除吗?')){
                            const id = $(this).parents('ul').data('id')
                            _this.remove(id)
                            header.changeNum()
                        }
                    }
                })
            }
            remove (id) {
                this.cart = this.cart.filter((shop) =>{
                    return shop.id != id
                })
                if(this.cart.length===0){
                    localStorage.removeItem('cart')
                }else{
                    localStorage.setItem('cart',JSON.stringify(this.cart))
                }
                this.rendonShopCar()
            }
            reduce () {
                var _this = this
                $('#goodlist').on('click','span',function() {
                    if($(this).hasClass('car-reduce')){
                        const id = $(this).parents('ul').data('id')
                        var temp = [];
                        _this.cart = _this.cart.map((shop) =>{
                            if(shop.id === id){
                                shop.count--;
                                
                                if(shop.count == 0 ){
                                    alert('亲，最少购买一件哦~')
                                    shop.count = 1
                                    $(this).removeClass('car-reduce')
                                }
                                $(this).next('input').val(shop.count)
                                var sum = shop.count * shop.price;
                                $(this).parent().next().find('span').html(sum.toFixed(2))
                                
                                // console.log(shop)
                            }
                            temp.push(shop);
                            return shop
                        })
                        _this.cart = temp;
                        localStorage.setItem('cart',JSON.stringify(_this.cart))
                        _this.celeMoney()
                    }
                })
                
            }
            add () {
                var _this = this
                $('#goodlist').on('click','span',function() {
                    if($(this).hasClass('car-add')){
                        const id = $(this).parents('ul').data('id')
                        _this.cart = _this.cart.map((shop) =>{
                            if(shop.id === id){
                                shop.count++
                                $(this).parents('ul').find('input').val(shop.count)
                                var sum = shop.count * shop.price
                                $(this).parent().next().find('span').html(sum.toFixed(2)) 
                                if(shop.count >=1){
                                    $('#shop-reduce').addClass('car-reduce')
                                }   
                            }
                                return shop
                            })
                        localStorage.setItem('cart',JSON.stringify(_this.cart))    
                        _this.celeMoney()
                    }
                })
            }
            celeMoney () {
                if(this.cart){
                    var figure = this.cart.reduce((sum,shop) =>{
                        if(shop.check){
                            sum += shop.count * shop.price
                        }
                        return sum 
                    },0)
                    $('.money span').html(figure.toFixed(2))
                    
                }
                

            }
            changeCheck () {
                var _this = this
                $('.one input').on('change',function (){
                    const id = $(this).parents('ul').data('id')
                    _this.cart = _this.cart.map(shop =>{
                        if(shop.id === id){
                            shop.check = $(this).prop('checked')
                        }
                        return shop
                    })
                    localStorage.setItem('cart',JSON.stringify(_this.cart))
                    _this.celeMoney()
                    _this.checkAll()
                })
            }
            checkAll() {
                var _this = this
                var ischeck = _this.cart.every(shop => shop.check)
                $('#checkA').prop('checked',ischeck)
                
                $('#checkA').on('change',function (){
                    if($(this).prop('checked')){
                        $('.one input').each((i,item) =>{
                            $(item).prop('checked',true)
                        })
                        _this.cart = _this.cart.map(shop =>{
                            shop.check = $(this).prop('checked')
                            return shop
                        })
                    }else{
                        $('.one input').each((i,item) =>{
                            $(item).prop('checked',false)
                        })
                        _this.cart = _this.cart.map(shop =>{
                            shop.check = $(this).prop('checked')
                            return shop
                        })
                    }
                    localStorage.setItem('cart',JSON.stringify(_this.cart))
                    _this.celeMoney()
                })
            }
        }
        new ShopCar()
    })
})